struct sBeamLight
{
	float3 Color;
	float3 StartPosition;
	float3 EndPosition;
	float FallOff;
	float Range;
};


float3 BeamLight_ComputeColor(sBeamLight instance, sMaterial material, float3 worldPosition, float3 worldNormal, float3 directionToCamera)
{
	// project the world position onto the beam line segment so we can treat that position as a point light
	float3 p0 = worldPosition;
	float3 p1 = instance.StartPosition;
	float3 p2 = instance.EndPosition;
	float3 d = p2 - p1;
	float lengthd = length(d);
	float t = -(dot(p1 - p0, d) / pow(lengthd, 2));
	t = clamp(t, 0.05, 0.95);
	float3 lightPoint = p1 + (d) * t;
	
	float3 lightVector = lightPoint - worldPosition;
    float lightDist = length(lightVector);
    float3 directionToLight = normalize(lightVector);
    
    //calculate the intensity of the light with exponential falloff
    float baseIntensity = pow(saturate((instance.Range - lightDist) / instance.Range), instance.FallOff);
    
    float diffuseIntensity = saturate( dot(directionToLight, worldNormal));
	float3 diffuse = diffuseIntensity * instance.Color * material.DiffuseColor;

    //calculate Phong components per-pixel
    float3 reflectionVector = normalize(reflect(-directionToLight, worldNormal));

    //calculate specular component
	float3 specular = saturate(instance.Color * material.SpecularColor * pow(saturate(dot(reflectionVector, directionToCamera)), material.SpecularPower));
	
	float3 color = 0;
	color += baseIntensity * (diffuse + specular);
	return  color;
}